package qldc.pm.ksdl.util;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import qldc.pm.csdl.cd90.model.CongDan;
import qldc.pm.csdl.cd90.model.jaxb.AnhChanDungJAXB;
import qldc.pm.csdl.cd90.model.jaxb.CMNDJAXB;
import qldc.pm.csdl.cd90.model.jaxb.CongDanJAXB;
import qldc.pm.csdl.cd90.model.jaxb.DanTocJAXB;
import qldc.pm.csdl.cd90.model.jaxb.GioiTinhJAXB;
import qldc.pm.csdl.cd90.model.jaxb.HoVaTenJAXB;
import qldc.pm.csdl.cd90.model.jaxb.LoaiDoiTuongJAXB;
import qldc.pm.csdl.cd90.model.jaxb.MaSoThueCaNhanJAXB;
import qldc.pm.csdl.cd90.model.jaxb.NgaySinhJAXB;
import qldc.pm.csdl.cd90.model.jaxb.NgheNghiepJAXB;
import qldc.pm.csdl.cd90.model.jaxb.NoiLamViecJAXB;
import qldc.pm.csdl.cd90.model.jaxb.NoiOHienTaiJAXB;
import qldc.pm.csdl.cd90.model.jaxb.NoiSinhJAXB;
import qldc.pm.csdl.cd90.model.jaxb.NoiThuongTruJAXB;
import qldc.pm.csdl.cd90.model.jaxb.QuanHeHuyetThongJAXB;
import qldc.pm.csdl.cd90.model.jaxb.QuanHeVoiChuHoJAXB;
import qldc.pm.csdl.cd90.model.jaxb.QueQuanJAXB;
import qldc.pm.csdl.cd90.model.jaxb.QuocTichJAXB;
import qldc.pm.csdl.cd90.model.jaxb.RootJAXB;
import qldc.pm.csdl.cd90.model.jaxb.SoHoChieuJAXB;
import qldc.pm.csdl.cd90.model.jaxb.TenGoiKhacJAXB;
import qldc.pm.csdl.cd90.model.jaxb.TheBaoHiemYTeJAXB;
import qldc.pm.csdl.cd90.model.jaxb.TinhTrangHonNhanJAXB;
import qldc.pm.csdl.cd90.model.jaxb.TonGiaoJAXB;
import qldc.pm.csdl.cd90.model.jaxb.TrinhDoChuyenMonJAXB;
import qldc.pm.csdl.cd90.model.jaxb.TrinhDoHocVanJAXB;
import qldc.pm.csdl.cd90.service.CongDanLocalServiceUtil;
import qldc.pm.csdl.cd90.util.CongDanJAXBUtil;
import qldc.pm.qtht.model.ChucNangPhanMem;
import qldc.pm.qtht.model.VaiTro;
import qldc.pm.qtht.service.NguoiDungLocalServiceUtil;
import qldc.pm.qtht.service.VaiTroLocalServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
import com.liferay.portal.kernel.util.DateUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;

public class KSDLUtil {
	
	/** The Constant LOG. */
	private static final Log LOG = LogFactoryUtil.getLog(KSDLUtil.class);
	
	/**
	 * Parse date follow the format: dd/MM/yyyy
	 * 
	 * @param date
	 * @return Date
	 * @throws ParseException
	 */
	public static Date parseDate(String date) throws ParseException{
		return parseDate(date, KSDLConstants.dd_MM_yyyy);
	}
	
	/**
	 * Parse date follow the pattern
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String dateString, String pattern) throws ParseException {
		DateFormat df = DateFormatFactoryUtil.getSimpleDateFormat(pattern);
		
		Date date = null;
		
		if(Validator.isNull(dateString)){
			return null;
		} else {
			try {
				date = df.parse(dateString);
			} catch (ParseException e) {
				throw e;
			}
		}
		
		return date;
	}
	
	/**
	 * get date String
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static String parseDateToString(Date date, String pattern, Locale locale) {
		String dateString = StringPool.BLANK;
		
		try {
			dateString = DateUtil.getDate(date, pattern, locale);
		} catch (Exception e) {
		}
		
		return dateString;
	}
	
	public static List<ChucNangPhanMem> getChucNangTheoNguoiDung(long userId){
		List<VaiTro> listVaiTro = NguoiDungLocalServiceUtil.getVaiTros(userId);
		HashMap<String, String> map = new HashMap<String, String>();
		
		List<ChucNangPhanMem> result = new ArrayList<ChucNangPhanMem>();
		
		if(null != listVaiTro && listVaiTro.size() > 0){
			for(VaiTro vaiTro : listVaiTro){
				List<ChucNangPhanMem> chucNangPhanMems = VaiTroLocalServiceUtil.getChucNangPhanMemsByVaiTroId(vaiTro.getVaiTroId());
					
				for(ChucNangPhanMem obj : chucNangPhanMems){
					if(!map.containsKey(obj.getMaChucNangPhanMem())){
						result.add(obj);
						map.put(obj.getMaChucNangPhanMem(), obj.getMaChucNangPhanMem());
					}
				}
			}
		}
		Collections.sort(result, new Comparator<ChucNangPhanMem>() {

			@Override
			public int compare(ChucNangPhanMem o1, ChucNangPhanMem o2) {
				// TODO Auto-generated method stub
				return (int) (o1.getChucNangPhanMemId() - o2.getChucNangPhanMemId());
			}
			
		});
		return result;
	}
	
	/**
	 * Export cong dan from cong dan list and write to file
	 * 
	 * @param congDanList
	 * @param xmlFile
	 * @throws JAXBException
	 * @throws SystemException 
	 */
	public static void exportCongDan(List<CongDan> congDanList, File xmlFile) throws JAXBException, SystemException{
		
		if(null != congDanList && !congDanList.isEmpty() && null != xmlFile){
			List<CongDanJAXB> congDanJAXBList = new ArrayList<CongDanJAXB>();
			RootJAXB root = new RootJAXB();
			
			for(CongDan congDan: congDanList){
				String soDinhDanhCN = congDan.getSoDinhDanhCaNhan();
				
				CongDanJAXB cdJaxb = CongDanJAXBUtil.convertDBObjectArrayToCongDanJAXB(CongDanLocalServiceUtil.findCongDanAndItsAttributesBySDDCN(soDinhDanhCN));
				if(null != cdJaxb){
					congDanJAXBList.add(cdJaxb);
				}
			}
			
			root.setCongDanList(congDanJAXBList);
			
			// create JAXB context and instantiate marshaller
		    JAXBContext context = JAXBContext.newInstance(RootJAXB.class);
		    Marshaller m = context.createMarshaller();
		    
		    // Format output: indent xml file
		    //m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

		    // Write to System.out
		    //m.marshal(root, System.out);

		    // Write to File
		    m.marshal(root, xmlFile);
		} else {
			LOG.error("CongDanList is (null or empty) or the specified file is null");
		}
	}
}
